local boyun = fk.CreateSkill{
  name = "lz__boyun",
}

Fk:loadTranslationTable{
  ["lz__boyun"] = "搏运",
  [":lz__boyun"] = "你可视为使用无次数限制的【杀】，目标需展示你至少一张手牌，并将之伤害改为因此展示的【杀】数，"..
  "若此【杀】造成伤害，你重铸所有【杀】，否则你获得每名目标角色一张牌，然后你选择一项：1.弃置至少一张【杀】，对目标角色各造成等量伤害；2.结束此回合。",
  
  ["#boyun-slash"] = "搏运：你可以视为使用一张无次数限制的【杀】",
  ["#boyun-show"] = "搏运：请展示 %src 的至少一张手牌，伤害将改为展示的【杀】数",
  ["#boyun-damage"] = "%from 的 %arg 被触发，对 %to 的伤害改为 %arg2 点",
  ["#boyun-discard"] = "搏运：你可以弃置至少一张【杀】，对目标角色各造成等量伤害",
  -- 语音台词
  ["$sd__boyun1"] = "",
  ["$sd__boyun2"] = "",
}

boyun:addEffect("viewas", {
  pattern = "slash",
  prompt = "#boyun-slash",
  card_filter = Util.FalseFunc,
  view_as = function(self, player, cards)
    if #cards ~= 0 then return end
    local slash = Fk:cloneCard("lvshi__slash")
    slash.skillName = boyun.name
    return slash
  end,
  enabled_at_play = Util.TrueFunc,
  enabled_at_response = function(self, player, response)
    return not response
  end,
})

boyun:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card)
    if player:hasSkill(boyun.name) and card and card.skillName == boyun.name and scope == Player.HistoryPhase then
      return true
    end
  end,
})

boyun:addEffect(fk.TargetSpecified, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(boyun.name) and data.card.trueName == "slash"
    and not player:isKongcheng() and data.card.skillName == boyun.name
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = data.to
    local cards = room:askToChooseCards(to, {
      target = player,
      min = 1,
      max = player:getHandcardNum(),
      prompt = "#boyun-show:" .. player.id,
      skill_name = boyun.name,
      flag = "h",
    })
    if #cards > 0 then
      player:showCards(cards)
      local slashCount = #table.filter(cards, function(id)
        return Fk:getCardById(id).trueName == "slash"
      end)
      room:sendLog{
        type = "#boyun-damage",
        from = player.id,
        to = {to.id},
        arg = boyun.name,
        arg2 = slashCount,
      }
      --data.additionalDamage = slashCount - 1
      data.card.extra_data = data.card.extra_data or {}
      data.card.extra_data.boyun = slashCount
    end
  end,
})

boyun:addEffect(fk.DamageCaused, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(boyun.name) and data.card and data.card.skillName == boyun.name
    and data.card.extra_data and data.card.extra_data.boyun
  end,
  on_use = function(self, event, target, player, data)
    local slashCount = data.card.extra_data.boyun
    data.damage = slashCount
  end,
})

boyun:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(boyun.name) and data.card and data.card.skillName == boyun.name
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.damageDealt then
      local slashs = table.filter(data.cards, function(id)
        return Fk:getCardById(id).trueName == "slash"
      end)
      room:recastCard(slashs, player, boyun.name)
    else
      local targets = data:getAllTargets()
      for _, p in ipairs(targets) do
        if not p:isNude() then
          local cid = room:askToChooseCard(player, { target = p, flag = "he", skill_name = boyun.name })
          room:obtainCard(player.id, cid, false, fk.ReasonPrey, player.id, boyun.name)
        end
      end
      local cards = room:askToDiscard(player, {
        min_num = 1,
        max_num = 999,
        include_equip = false,
        skill_name = boyun.name,
        cancelable = true,
        pattern = "slash",
        prompt = "#boyun-discard",
      })
      if #cards > 0 then
        for _, p in ipairs(targets) do
          room:damage({
            from = player,
            to = p,
            damage = #cards,
            skillName = boyun.name,
          })
        end
      else
        room:endTurn()
      end
    end
  end,
})

return boyun